package com.imy.accessibility.node;

import android.animation.ValueAnimator;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.imy.accessibility.CoreService;
import com.imy.accessibility.R;
import com.imy.accessibility.bean.CustomNode;
import com.imy.accessibility.dialog.NodeInfoDialog;
import com.imy.common.base.BaseCustomViewGroup;
import com.imy.common.util.ALog;
import com.imy.common.util.ClipboardUtils;
import com.imy.common.util.ToastUtil;
import com.imy.common.util.UiUtil;
import com.imy.lua.lib.node.UDRule;

import java.util.ArrayList;
import java.util.List;

/**
 * create by Imy
 */
public class ControlUiView extends BaseCustomViewGroup {
    private TextView backLastNodeTv;
    private TextView goNextNodeTv;

    private TextView checkCurrentNodeTv;

    private LinearLayout controlUiLl;
    private ListView listView;
    private ControlUiNodeAdapter adapter;


    private LinearLayout bottomLl;

    public ControlUiView(Context context) {
        super(context);
    }

    public ControlUiView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ControlUiView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(View inflate) {
        goNextNodeTv = inflate.findViewById(R.id.go_next_node_tv);
        backLastNodeTv = inflate.findViewById(R.id.back_last_node_tv);
        checkCurrentNodeTv = inflate.findViewById(R.id.check_current_node_tv);

        controlUiLl = inflate.findViewById(R.id.control_ui_ll);
        listView = inflate.findViewById(R.id.control_list_view);
        inflate.findViewById(R.id.copy_id).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mNodeInfo.mViewIdResourceName)){
                    ToastUtil.toast(mContext,R.string.red_id_is_empty_cant_copy);
                    return;
                }
                ClipboardUtils.setText(mContext, mNodeInfo.mViewIdResourceName);
                ToastUtil.toast(mContext,R.string.copy_success);
                hide();
            }
        });
        bottomLl = inflate.findViewById(R.id.bottom_ll);
        inflate.findViewById(R.id.control_list_space_view).
                setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        hide();
                    }
                });
        goNextNodeTv.setOnClickListener(v -> goNextNode());
        backLastNodeTv.setOnClickListener(v -> {
            backLastNode();
        });
        checkCurrentNodeTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();

                NodeInfoDialog nodeInfoDialog = new NodeInfoDialog((AppCompatActivity) mContext);
                nodeInfoDialog.show(mNodeInfo);
            }
        });

    }

    private void hideBottom() {
        bottomLl.setVisibility(GONE);
    }

    public void showBottom() {
        bottomLl.setVisibility(VISIBLE);
    }

    private void hide() {
        startAnim(0, UiUtil.getDeviceHeight(getContext()), false);
    }

    private void startAnim(int start, int end, boolean show) {

        setVisibility(VISIBLE);
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        valueAnimator.addUpdateListener(animation -> {
            int animatedValue = (int) animation.getAnimatedValue();
            controlUiLl.setY(animatedValue);
            if (animation.getAnimatedFraction() == 1) {
                if (show) {
                    setVisibility(VISIBLE);
                } else {
                    setVisibility(GONE);
                }
            }
        });
        valueAnimator.setDuration(300);
        valueAnimator.start();
    }

    private void backLastNode() {

        CustomNode parent = mNodeInfo.parent;
        if (parent == null) {
            ToastUtil.toast(getContext(), getResources().getString(R.string.last_node_empty));
            return;
        }
        if (parent.parent == null) {
            ToastUtil.toast(getContext(), getResources().getString(R.string.last_node_empty));
            return;
        }
        hide();
        if (listener != null) {
            listener.onBackLastNode(parent.parent);
        }
    }

    private void goNextNode() {
        int childCount = mNodeInfo.getChildCount;
        if (childCount == 0) {
            ToastUtil.toast(getContext(), R.string.can_have_next_node);
            return;
        }
        hide();
        if (listener != null) {
            listener.onGoNextNode(mNodeInfo);
        }
    }

    public void show(CustomNode nodeInfo) {
        mNodeInfo = nodeInfo;
        startAnim(UiUtil.getDeviceHeight(mContext), 0, true);
        setNodeList();
    }

    private void setNodeList() {
        List<CustomNode> list = new ArrayList<>();

        for (int i = 0; i < mNodeInfo.getChildCount; i++) {
            CustomNode child = mNodeInfo.child.get(i);
            list.add(child);
        }
        if (adapter == null) {
            adapter = new ControlUiNodeAdapter(mContext, list);
            listView.setAdapter(adapter);
        } else {
            adapter.setLists(list);
        }
    }

    private CustomNode mNodeInfo;

    @Override
    protected int getLayoutID() {
        return R.layout.custom_view_control_node;
    }

    public interface ControlUiListener {
        void onBackLastNode(CustomNode nodeInfo);

        void onGoNextNode(CustomNode nodeInfo);


    }

    public void setControlUiListener(ControlUiListener controlUiListener) {
        this.listener = controlUiListener;
    }

    private ControlUiListener listener;


}
